package com.qifan.energy.activity;

import android.os.Bundle;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.qifan.energy.base.BaseActivity;
import com.qifan.energy.bean.result.BalanceHistorySearchItem;
import com.qifan.energy.R;
import com.qifan.energy.adapter.RoomEarningsListAdapter;
import com.qifan.energy.bean.result.Page2;
import com.qifan.energy.http.net.NetUtils;
import com.qifan.energy.http.response.MyObserver;
import com.qifan.energy.util.ToastUtils;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class RoomEarningsActivity extends BaseActivity {

    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;

    private List<BalanceHistorySearchItem> dataList = new ArrayList();
    RoomEarningsListAdapter adapter;
    private int pageNum = 1;

    /*@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_about);
    }*/

    @Override
    protected int getLayoutId() {
        return R.layout.activity_room_earnings;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        /* 更改状态栏 */
        changeStatusTransparentFontBlack();

        adapter = new RoomEarningsListAdapter(dataList);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(adapter);
        recyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(this)
                .sizeResId(R.dimen.dp_0_5)
                .colorResId(R.color.divider_color)
                .build());
        /* 加载更多 */
        adapter.setOnLoadMoreListener(()->{
            memberBalanceHistorySearchMore();
        },recyclerView);

        //添加空布局
        adapter.setEmptyView(getEmptyDataView());
    }

    @Override
    protected void initData() {
        memberBalanceHistorySearch();
    }

    /* 获取空布局 */
    private View getEmptyDataView() {
        return getLayoutInflater().inflate(R.layout.roomearnings_list_empty, recyclerView, false);
    }

    //查看余额变动记录（房间收益列表）
    /* 下来刷新 */
    private void memberBalanceHistorySearch(){
        pageNum = 1;
        NetUtils.memberBalanceHistorySearch(0, 2, pageNum, Page2.PAGESIZE_10, new MyObserver<Page2<BalanceHistorySearchItem>>(this) {
            @Override
            public void onSuccess(Page2<BalanceHistorySearchItem> page2) {
                if (null != page2) {
                    dataList = page2.getList();
                    adapter.setNewData(page2.getList());
                }
            }

            @Override
            public void onFailure(Throwable e, String errorMsg) {
                ToastUtils.showShort(errorMsg);
            }
        });
    }

    /* 上拉加载 */
    private void memberBalanceHistorySearchMore(){
        ++pageNum;
        NetUtils.memberBalanceHistorySearch(0, 2, pageNum, Page2.PAGESIZE_10, new MyObserver<Page2<BalanceHistorySearchItem>>(this) {
            @Override
            public void onSuccess(Page2<BalanceHistorySearchItem> page2) {
                if (null != page2) {
                    dataList.addAll(page2.getList());
                    boolean isNoMore = (dataList.size() >= page2.getTotal());
                    adapter.setEnableLoadMore(!isNoMore);
                    if (isNoMore) {
                        adapter.loadMoreEnd();
                    } else {//刷新数据
                        adapter.setNewData(dataList);
                    }
                }
            }

            @Override
            public void onFailure(Throwable e, String errorMsg) {
                ToastUtils.showShort(errorMsg);
            }
        });
    }
}
